package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.AttrValueDao;
import com.example.child_protection.dto.info.AttrValueDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.AttrValueRequestDTO;
import com.example.child_protection.dto.request.find.CheckCodeRequestDTO;
import com.example.child_protection.dto.response.AttrValueListResponseDTO;
import com.example.child_protection.dto.response.AttrValueResponseDTO;
import com.example.child_protection.dto.response.CheckCodeResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.AttrModel;
import com.example.child_protection.model.AttrValueModel;
import com.example.child_protection.service.AttrValueQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 属性候选值查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class AttrValueQueryServiceImpl extends MyBaseService implements AttrValueQueryService {

    @Autowired
    public AttrValueDao attrValueDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public AttrValueResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ATTR_VALUE_FIND_ID, requestDTO, AttrValueResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, AttrValueResponseDTO>() {
            @Override
            protected AttrValueResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                AttrValueResponseDTO responseDTO = new AttrValueResponseDTO();
                AttrValueModel attrValueModel = attrValueDao.findId(requestDTO.getId());
                AttrValueDTO attrValue = new AttrValueDTO();
                BeanUtils.copyProperties(attrValueModel, attrValue);
                responseDTO.setInfo(attrValue);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public AttrValueListResponseDTO findList(AttrValueRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ATTR_VALUE_FIND_LIST, requestDTO, AttrValueListResponseDTO.class, new MyServiceExecutor<AttrValueRequestDTO, AttrValueListResponseDTO>() {
            @Override
            protected AttrValueListResponseDTO myprocess(AttrValueRequestDTO requestDTO) throws Exception {
                AttrValueListResponseDTO responseDTO = new AttrValueListResponseDTO();
                AttrValueModel req = new AttrValueModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<AttrValueModel> lists = attrValueDao.findPage(req);
                List<AttrValueDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), AttrValueDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 检测CODE是否重复
     * @param requestDTO
     * @return
     */
    public CheckCodeResponseDTO checkCode(CheckCodeRequestDTO requestDTO){
        return this.service(ServiceActionEnums.ATTR_CHECK_CODE, requestDTO, CheckCodeResponseDTO.class, new MyServiceExecutor<CheckCodeRequestDTO, CheckCodeResponseDTO>() {
            @Override
            protected CheckCodeResponseDTO myprocess(CheckCodeRequestDTO requestDTO) throws Exception {
                CheckCodeResponseDTO res = new CheckCodeResponseDTO();
                AttrValueModel req = new AttrValueModel();
                req.setValKey(requestDTO.getCode());
                int result = attrValueDao.count(req);
                if (result > 0) {
                    res.setFlag(BooleanEnums.T);
                } else {
                    res.setFlag(BooleanEnums.F);
                }
                res.successfully();
                return res;
            }
        });
    }
}
